package com.example.idepluginlearn;

import com.example.idepluginlearn.entity.DataCenter;
import com.example.idepluginlearn.entity.NoteData;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.EditorTextField;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;

public class AddNoteDialog extends DialogWrapper {
    private static final Logger log = LoggerFactory.getLogger(AddNoteDialog.class);
    private EditorTextField tfTitle;
    private EditorTextField tfMark;

    protected AddNoteDialog() {
        super(true);
        setTitle("添加笔记注释!");
        init();
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        tfTitle = new EditorTextField("笔记标题");
        tfMark = new EditorTextField("笔记内容");
        tfMark.setPreferredSize(new Dimension(200, 100));

        panel.add(tfTitle, BorderLayout.NORTH);
        panel.add(tfMark, BorderLayout.CENTER);
        return panel;
    }


    @Override
    protected JComponent createSouthPanel() {
        JPanel panel = new JPanel();
        JButton button = new JButton("添加笔记到列表");
        button.addActionListener(e-> {
            String title = tfTitle.getText();
            String mark = tfMark.getText();

            NoteData noteData = new NoteData(title, mark, DataCenter.SELECT_TEXT, DataCenter.FILE_NAME, DataCenter.FILE_NAME.substring(DataCenter.FILE_NAME.lastIndexOf(".") + 1));
            DataCenter.NOTE_LIST.add(noteData);

            DataCenter.TABLE_MODEL.addRow(DataConvert.convert(noteData));
        });
        panel.add(button);
        return panel;
    }
}

